package org.ezplatform.office.meeting.setting.base.web.rest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.service.*;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.meeting.common.util.QSApiThread;
import org.ezplatform.office.meeting.info.entity.QSUser;
import org.ezplatform.office.meeting.info.service.QSUserService;
import org.ezplatform.office.meeting.search.service.SearchService;
import org.ezplatform.office.meeting.setting.base.entity.BaseSet;
import org.ezplatform.office.meeting.setting.base.service.BaseSetService;
import org.ezplatform.util.GlobalConstant;
import org.ezplatform.util.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quanshi.openapi.QsAPI;
import com.quanshi.openapi.QsUtil;
import com.quanshi.openapi.entity.QsAccount;
import com.quanshi.openapi.entity.QsConfParty;
import com.quanshi.openapi.entity.QsConfSetting;
import com.quanshi.openapi.entity.QsConsts;
import com.quanshi.openapi.entity.QsResponse;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: MeetingQSWebApiController
 * @Description: 全时会议接口类
 * @author: sj
 * @date 2020-04-11 
 */
@RestController
@RequestMapping(value = "/api/w/meetingQSWebApi")
public class MeetingQSWebApiController extends BaseController<BaseSet, String> {
	
	private final static Logger logger = LoggerFactory.getLogger(MeetingQSWebApiController.class);
	
	// 产品ID, 5:MAX 2:PC2.0预约 3:PC2.0即时 4:PC2.0统一
	private static final String appId = "6";
	
	private static String p12Path =  GlobalConstant.getConfig("qs.client");// "D:\\workspacests3.9.4\\openapi-sample\\zhongkeyungu.client.p12";
	private static String trustStorePath =GlobalConstant.getConfig("qs.server");//"D:\\workspacests3.9.4\\openapi-sample\\server.truststore";
	private static String emailstr =GlobalConstant.getConfig("qs.email");//qusnshi.com
	
	@Autowired
	private BaseSetService service;
	
	@Autowired
	private QSUserService userservice;
	
	@Override
	protected BaseService<BaseSet, String> getEntityService() {
		return service;
	}
    
    
    @ApiOperation(value = "注册用户账号", notes = "注册用户账号")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userName", value = "账号", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "hostUserId", value = "主账号", required = true, paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "siteUrl", value = "账号所属站点", required = true, paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "loginName", value = "注册账号", required = true, paramType = "query", dataType = "String") })
	@RequestMapping(value = "/createAccounts", method = RequestMethod.POST)
	public String createAccounts(HttpServletRequest request) {
    	logger.debug("-----createAccount----");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String hostUserId = request.getParameter("hostUserId");
		String siteUrl = request.getParameter("siteUrl");
		logger.debug("userName:"+userName);
		logger.debug("password:"+password);
		logger.debug("hostUserId:"+hostUserId);
		logger.debug("siteUrl:"+siteUrl);
		
		logger.debug("p12Path:"+p12Path);
		logger.debug("trustStorePath:"+trustStorePath);
		String result = "";
		
		try {
			List uList = service.getAllUserAcc();
			if(uList!=null&&uList.size()>0) {

				QSApiThread smt = new QSApiThread(userName, password, hostUserId, 
						                          siteUrl, appId, p12Path, 
						                          trustStorePath, emailstr,uList);
		    	smt.start();
		    	result = "{\"success\":\"0\",\"synctime\":\""+(uList.size()+1)+"\"}";
			}else {
				result = "{\"success\":\"0\",\"synctime\":\"0\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"success\":\"-1\"}";
		}
		
//		try {
//			
//			// 初始化OpenAPI
//			// 请确保证书路径正确
//			QsAPI.init(appId, siteUrl, p12Path, trustStorePath);
//			// 账号登陆
//			userservice.login(userName, password, false);
//			
//			String company="wanhu";
//
//			//Map<String, Object> qsUserMap = createAccount(userName, password, hostUserId,company,"JSH3");
//			//logger.debug("qs_userid:"+qsUserMap.get("userId"));
//			//logger.debug("qs_passwd:"+qsUserMap.get("passwd"));
//			
//			List qsUserList = new ArrayList();
//			List list = service.getAllUserAcc();
//			if(list!=null) {
//				Map map = null;
//				Map qsmap = null;
//				QSUser user = null;
//				
//				String login_name = "";
//				String user_name = "";
//				String userId = "";
//				String emailacc = "";
//				for(int i=0;i<list.size();i++) {
//					map = (Map) list.get(i);
//					
//					login_name = map.get("login_name")+"";
//					user_name = map.get("user_name")+"";
//					userId = map.get("id")+"";
//					emailacc = login_name+"@"+emailstr;
//					logger.debug("login_name:"+login_name);
//					//创建账号
//					qsmap = userservice.createAccount(userName, password, hostUserId, company, login_name, user_name, userId, emailacc);
//					
//					if(qsmap!=null) {
//					   
//					   user = new QSUser();
//					   user.setQsuserId(qsmap.get("userId")+"");
//					   user.setQspasswd(qsmap.get("passwd")+"");
//					   user.setUserAccount(map.get("login_name")+"");
//					   user.setUserId(userId);
//					   user.setQsuserName(user_name);
//					   user.setQsemail(emailacc);
//					   qsUserList.add(user);
//					}
//				}
//			}
//			
//			if(qsUserList!=null&&qsUserList.size()>0) {
//				userservice.saveQSUserList(qsUserList);
//			}
//			result = "{\"success\":\"0\"}";
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//			result = "{\"success\":\"-1\"}";
//		}
		return result;
	}
    
}
